package com.example.ship.entity;

import lombok.Data;

/**
 * 军舰
 */
@Data
public class Ship {

    private Location location; // 位置
    private Planet planet;

    public Ship(Location location) {
        this.location = location;
    }

    public Ship(Location location, Planet planet) {
        this.location = location;
        this.planet = planet;
    }

    /**
     * 军舰向前移动
     * @return
     */
    public boolean moveForward() {
        return location.forward(planet.getMax());
    }

    /**
     * 军舰向后移动
     */
    public boolean moveBackward() {
        return location.backward();
    }

    /**
     * 军舰向左移动
     */
    public void moveTurnLeft() {
        location.turnLeft();
    }

    /**
     * 军舰向右移动
     */
    public void moveTurnRight() {
        location.turnRight();
    }

    /**
     * 根据接收的命令字符串依次执行对应的移动
     * @param commands
     */
    public void receiveCommands(String commands) {
        for (char command : commands.toCharArray()) {
            switch(command) {
                case 'F':
                    moveForward();
                    break;
                case 'B':
                    moveBackward();
                    break;
                case 'L':
                    moveTurnLeft();
                    break;
                case 'R':
                    moveTurnRight();
                    break;
            }
        }
    }
}
